<?php
namespace App\Logic\Spider;

use GuzzleHttp\Client;
use Illuminate\Support\Arr;
use GuzzleHttp\Exception\RequestException;
class CrawlCReviews
{
    protected static $_userAgent = [
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0',
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0',
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6',
        'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36',
        'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1',
        'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0',
        'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
        'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
        'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0',
        'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11',
    ];

    private static $_header = [
        'Accept'                    => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Language'           => 'zh-CN,zh;q=0.9',
//        'Accept-Encoding'           => 'gzip, deflate, br',
        'Accept-Encoding'           => 'gzip, deflate',
        'Cookie'                    => true,
        'Upgrade-Insecure-Requests' => '1',
    ];

    public static function crawl(){
        return self::exeCrawl();
    }

    private static function exeCrawl(){
        $header = self::$_header;
        $message = [];
        try{
            $ten = ['https://www.amazon.com/gp/customer-reviews/RSG604WXRQBW2/ref=cm_cr_othr_d_rvw_ttl?ie=UTF8&ASIN=B08V4H741T'];
            foreach ($ten as $_ten){
                $header['User-Agent'] = Arr::random(self::$_userAgent);
                $client = new Client([
                    'timeout' => 5,
                    'cookies' => true,
                    'headers'=> $header,
                ]);
                $res = $client->get($_ten);
                $str = $res->getBody()->getContents();
                dd($str);
                sleep(10);
            }
            return $message;
        }catch(RequestException $e){
            dd($e->getMessage());
            $message['RequestException'] = 'Http Error';
            return $message;
        }
    }
}
